package com.luff.thread.task;

import com.luff.thread.ThreadPool.MyThreadPoolExecutor;

/**
 * Create By The-Long time 2021/10/6 20:08
 **/

public class MyTask implements Runnable {

    //线程名称
    private String name;

    //构造任务
    public MyTask(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        //没加锁,存在资源读写异常
        while(!MyThreadPoolExecutor.isPause){
            if(MyThreadPoolExecutor.isDied){
                return;
            }
            try {
                System.out.println(Thread.currentThread().getName() + " 正在放水!");
                MyThreadPoolExecutor.water -= MyThreadPoolExecutor.waterDischarge;

                Thread.sleep(200); //让任务执行慢点
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("暂停放水中。。。");
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "my-thread"+name;
    }
}